package org.example.mall.admin.controller.manage;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.example.mall.admin.constant.vaild.C;
import org.example.mall.admin.constant.vaild.Q;
import org.example.mall.admin.constant.vaild.U;
import org.example.mall.admin.model.entity.manage.SysLabel;
import org.example.mall.admin.service.manage.SysLabelService;
import org.example.mall.admin.util.BusOp;
import org.example.mall.admin.util.PermissionUtil;
import org.example.mall.admin.util.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 系统标签 前端控制器
 * </p>
 *
 * @author hzq
 * @since 2022-01-24
 */
@RestController
@RequestMapping("/manage/sysLabel")
@AllArgsConstructor
public class SysLabelController {


    private final SysLabelService labelService;

    @GetMapping("/{id}")
    @ApiOperation("通过id查询标签信息")
    public R<SysLabel> getById(@PathVariable Integer id) {
        return R.ofSuccess(labelService.getById(id));
    }

    @GetMapping
    @ApiOperation("返回标签全部集合")
    public R<List<SysLabel>> list(@Validated(Q.class) SysLabel label) {
        LambdaQueryWrapper<SysLabel> wrapper = Wrappers.<SysLabel>lambdaQuery()
                .eq(SysLabel::getLabelType, label.getLabelType()).orderByAsc(SysLabel::getSort);
        if (StrUtil.isNotBlank(label.getExtra())) {
            wrapper.eq(SysLabel::getExtra, label.getExtra());
        }
        return R.ofSuccess(labelService.list(wrapper));
    }

    @GetMapping("/page")
    @ApiOperation("分页返回标签集合")
    public R<IPage<SysLabel>> page(Page page, @Validated(Q.class) SysLabel label) {
        LambdaQueryWrapper<SysLabel> wrapper = Wrappers.<SysLabel>lambdaQuery()
                .eq(SysLabel::getLabelType, label.getLabelType()).orderByAsc(SysLabel::getSort);
        if (StrUtil.isNotBlank(label.getExtra())) {
            wrapper.eq(SysLabel::getExtra, label.getExtra());
        }
        return R.ofSuccess(labelService.page(page, wrapper));
    }

    @PostMapping
    @ApiOperation("创建标签")
    public R<Boolean> add(@RequestBody @Validated(C.class) SysLabel sysLabel) {
        PermissionUtil.checkSysLabel(sysLabel.getLabelType(), "add");
        return R.ofSuccess("创建标签成功",
                BusOp.mustSucceed(labelService.save(sysLabel), "创建标签失败"));

    }

    @PutMapping
    @ApiOperation("修改标签")
    public R<Boolean> edit(@RequestBody @Validated(U.class) SysLabel sysLabel) {
        PermissionUtil.checkSysLabel(sysLabel.getLabelType(), "edit");
        return R.ofSuccess("修改标签成功",
                BusOp.mustSucceed(labelService.updateById(sysLabel), "修改标签失败"));
    }

    @ApiOperation("删除标签")
    @DeleteMapping("/{id}")
    public R<Boolean> removeById(@PathVariable Integer id) {
        SysLabel sysLabel = labelService.getById(id);
        BusOp.mustNotNull(sysLabel, "标签不存在");
        PermissionUtil.checkSysLabel(sysLabel.getLabelType(), "del");
        return R.ofSuccess("删除标签成功",
                BusOp.mustSucceed(labelService.del(sysLabel), "删除标签失败"));
    }

}

